/**
 * 广播插件
 * 避免冲突 取名为broadc
 */

function BroadC() {
}

BroadC.inherit(Control);

BroadC.prototype.init = function(oComponent) {
    BroadC.base.init.call(this);
    this._component = oComponent;
}



/**
 * 加载广播插件
 */
BroadC.prototype.create = function() {
    var id = Math.uuid();
    this._html = $('<div class="broadc-body"><object  type="application/x-fbframeworkplugin"  id="'+id +'" width="1" height="1"></object></div>')[0];
    this._plugin = $('object', this._html)[0];
    this._plugin.id = id;
    this._code = null;
    this._loginInfo = null;
    this._loginStatus = false;

}

BroadC.prototype.initLoad = function() {
    //加载插件
    var paramsObj = {};
    paramsObj["plugin_name"] ="IOTP_FB_ItcIPCast.dll";
    paramsObj["plugin_type_uuid"] ="7ad3b7d1-d7a2-4f02-877f-6524f508871f";
    paramsObj["net_url"] =this._plugin.id;
    paramsObj["local_dir"] =this._plugin.id;
    pluginHander( this._plugin,"framework","loadplugin",paramsObj,function(oResult){});
    //监听页面关闭注销插件
    //this.bindWindowDestroy(window);
}

/**
 *
 * 登录广播
 * @param id 设备id
 * @param oCallback 回调函数
 * @returns {*} 操作调用是否成功
 * @constructor
 */
BroadC.prototype.LoginIn = function(id,oCallback) {
    if(!id) return false;
    var soap = new HWSOAP("./broadcast/getBroadcastPlugin.do");
    soap.send({id:id});
    var oResult = soap.getResult();
    if(!oResult || !oResult.result ||
        !oResult.result["终端参数"] || !oResult.result["配置参数"]) return false;
    this._code = oResult.result["终端参数"];
    this._loginInfo = oResult.result["配置参数"];
    var loginfo = this._loginInfo.split(";");
    var params = {ip_str:loginfo[0],name_str:loginfo[1],passwd_str:loginfo[2]};
    //执行插件登录操作
    var execres = pluginHander(this._plugin,"do_general_op","login_in",params,oCallback);
    if(!execres.result){
        //调用登录操作失败
        console.log(execres.error);
    }
    return execres.result;
}

/**
 * 登出广播
 * @param oCallback 回调函数
 * @returns {*} 操作调用是否成功
 * @constructor
 */
BroadC.prototype.LoginOut = function(oCallback) {
    //执行插件登出操作
    var execres = pluginHander(this._plugin,"do_general_op","login_out",null,oCallback);
    if(!execres.result){
        //调用登出操作失败
        console.log(execres.error);
    }
    return execres.result;
}

/**
 * 开始双向语音对讲
 * @param oCallback 回调函数
 * @returns {*} 操作调用是否成功
 * @constructor
 */
BroadC.prototype.StartVoiceTalk = function(oCallback){
    var execres = pluginHander(this._plugin,"do_talk_op","talk_start",{term_id:this._code},oCallback);
    if(!execres.result){
        //调用开始对讲操作失败
        console.log(execres.error);
    }
    return execres.result;
}


/**
 * 停止双向语音对讲
 * @param oCallback 回调函数
 * @returns {*} 操作调用是否成功
 * @constructor
 */
BroadC.prototype.StopVoiceTalk = function(oCallback){
    var execres = pluginHander(this._plugin,"do_talk_op","talk_stop",null,oCallback);
    if(!execres.result){
        //调用停止对讲操作失败
        console.log(execres.error);
    }
    return execres.result;
}


/**
 * 开始语音广播
 * @param oCallback 回调函数
 * @returns {*} 操作调用是否成功
 * @constructor
 */
BroadC.prototype.StartVoiceBroadcast = function(oCallback) {
    var execres = pluginHander(this._plugin,"do_broadcast_op","broadcast_start",{term_id:this._code},oCallback);
    if(!execres.result){
        //调用开始语音广播操作失败
        console.log(execres.error);
    }
    return execres.result;
}

/**
 * 停止语音广播
 * @param oCallback 回调函数
 * @returns {*} 操作调用是否成功
 * @constructor
 */
BroadC.prototype.StopVoiceBroadcast = function(oCallback){
    var execres = pluginHander(this._plugin,"do_broadcast_op","broadcast_stop",null,oCallback);
    if(!execres.result){
        //调用停止语音广播操作失败
        console.log(execres.error);
    }
    return execres.result;
}

/**
 * 开始文件广播
 * @param url G://ghsy.mp3
 * @param oCallback 回调函数
 * @returns {*} 操作调用是否成功
 * @constructor
 */
BroadC.prototype.StartFileBroadcast = function(url,oCallback) {
    var params = {term_list:this._code,file_path:url};
    var execres = pluginHander(this._plugin,"do_local_file_op","start_play_local_file",params,oCallback);
    if(!execres.result){
        //调用开始文件广播操作失败
        console.log(execres.error);
    }
    return execres.result;
}

/**
 * 停止文件广播
 */
//BroadC.prototype.StopFileBroadcast  = function(){
//	this._plugin.StopFileBroadcast();
//}

/**
 * 停止文件广播
 * @param oCallback 回调函数
 * @returns {*} 操作调用是否成功
 * @constructor
 */
BroadC.prototype.StopBroadcast  = function(oCallback){
    var execres = pluginHander(this._plugin,"do_local_file_op","stop_play_local_file",null,oCallback);
    if(!execres.result){
        //调用停止文件广播操作失败
        console.log(execres.error);
    }
    return execres.result;
}

/**
 * 暂停文件广播
 * @param oCallback 回调函数
 * @returns {*} 操作调用是否成功
 * @constructor
 */
BroadC.prototype.PauseBroadcast  = function(oCallback){
    var execres = pluginHander(this._plugin,"do_local_file_op","pause_play_local_file",null,oCallback);
    if(!execres.result){
        //调用暂停文件广播操作失败
        console.log(execres.error);
    }
    return execres.result;
}

/**
 * 恢复播放
 * @param oCallback 回调函数
 * @returns {*} 操作调用是否成功
 * @constructor
 */
BroadC.prototype.ResumeBroadcast  = function(oCallback){
    var execres = pluginHander(this._plugin,"do_local_file_op","resume_play_local_file",null,oCallback);
    if(!execres.result){
        //调用恢复播放操作失败
        console.log(execres.error);
    }
    return execres.result;
}


/**
 * 设置音量
 * @param num 声音大小  0-100
 * @param oCallback 回调函数
 * @returns {*} 操作调用是否成功
 * @constructor
 */
BroadC.prototype.SessionVol  = function(num,oCallback){
    var execres = pluginHander(this._plugin,"do_general_op","set_session_volume",{session_vol:num+""},oCallback);
    if(!execres.result){
        //调用设置音量操作失败
        console.log(execres.error);
    }
    return execres.result;
}


/**
 * 下载播放文件
 * @param path 网络路径文件保存路径
 * @param oCallback 回调函数
 * @returns {*} 操作调用是否成功
 * @constructor
 */
BroadC.prototype.DownloadURLFile  = function(path,oCallback){
    var execres = pluginHander(this._plugin,"do_remote_file_op","remote_file_start",{file_url:path},oCallback);
    if(!execres.result){
        //调用下载播放文件操作失败
        console.log(execres.error);
    }
    return execres.result;
}


/**
 * 是否已登录
 * @returns {boolean}
 */
BroadC.prototype.isLogIn = function() {
    return this._loginStatus;
}

/**
 * 设置登录状态
 * @param status
 */
BroadC.prototype.setLoginStatus = function(status) {
    this._loginStatus = status;
}


/**
 * 绑定页面销毁事件
 */
BroadC.prototype.bindWindowDestroy = function (_window) {
    var that = this;
    $(_window).bind('beforeunload', function () {
        that.destroy();
    });
}

	
/**
 * 销毁插件
 */
BroadC.prototype.destroy = function () {
	 //this.LoginOut();
	 try {
		 delete this._plugin;
		 this._html.parentNode.removeChild(this._html);
     } catch (e) {
         console.log("清空元素失败");
     }
}

/**
 * 开始语音的声音播放(本地声音)
 */
BroadC.prototype.StartVoiceQuiet = function(){
    this._plugin.StartVoiceQuiet();
}

/**
 * 停止语音的声音播放(本地声音)
 */
BroadC.prototype.StopVoiceQuiet = function() {
    this._plugin.StopVoiceQuiet();
}
